package com.lzp.badminton.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.lzp.badminton.config.ResultMessage;
import com.lzp.badminton.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 腾讯云COS上传
 *
 * @param file
 * @return
 * @throws Exception
 */
@Controller
public class FileUploadController  {
    @Autowired
    private ResultMessage resultMessage;
    @Autowired
    private FileUploadService fileUploadService;

    @PostMapping(value = "/uploadmfile")
    @ResponseBody
    public ResultMessage uploadmultipartFile(@RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("1111111");
        String filePath = fileUploadService.upload(file);

        Map fileMap = new HashMap();
        fileMap.put("filePath", filePath);

        if (filePath != null && !filePath.equals("")) {
            resultMessage.success("200","文件上传成功", fileMap);
        } else {
            resultMessage.fail("404", "文件上传失败");
        }
        return resultMessage;
    }

    @PostMapping(value = "/uploadfile")
    @ResponseBody
    public ResultMessage uploadfile(@RequestParam("file") File file) throws Exception {
        String filePath = fileUploadService.upload(file);

        Map fileMap = new HashMap();
        fileMap.put("filePath", filePath);

        if (filePath != null && !filePath.equals("")) {
            resultMessage.success("200","文件上传成功", fileMap);
        } else {
            resultMessage.fail("404", "文件上传失败");
        }
        return resultMessage;
    }
}
